package com.api;

import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created with IDEA
 * Author: LuoHengYi
 * Date: 2022/6/14
 * Time: 15:09
 */
public class MessageBuilder {
    /**
     * 消息唯一id
     */
    private String messageId;


    /* 消息的主题：可理解为消息的key，用于匹配某条消息的生产者和消费者*/
    private String topic;

    /* 消息的路由规则，exchange 根据 routingKey 发送到那个queue*/
    private String routingKey = "";

    /* 消息携带的属性 */
    private Map<String, Object> attributes = new HashMap<>();

    /* 消息延迟的配置 */
    private int delayMills;

    /*消息类型 默认为可靠性消息*/
    private String messageType = MessageType.CONFIRM;

    public MessageBuilder() {
    }

    public MessageBuilder(String messageId, String topic, String routingKey, Map<String, Object> attributes, int delayMills, String messageType) {
        this.messageId = messageId;
        this.topic = topic;
        this.routingKey = routingKey;
        this.attributes = attributes;
        this.delayMills = delayMills;
        this.messageType = messageType;
    }

    public static MessageBuilder create() {
        return new MessageBuilder();
    }

    public MessageBuilder withMessageId(String messageId) {
        this.messageId = messageId;
        return this;
    }

    public MessageBuilder withTopic(String topic) {
        this.topic = topic;
        return this;
    }

    public MessageBuilder withRoutingKey(String routingKey) {
        this.routingKey = routingKey;
        return this;
    }

    public MessageBuilder withDelayMills(int delayMills) {
        this.delayMills = delayMills;
        return this;
    }

    public MessageBuilder withMessageType(String messageType) {
        this.messageType = messageType;
        return this;
    }

    public MessageBuilder withAttributes(String key,String value) {
        this.attributes.put(key,value);
        return this;
    }

    public Message build(){

        if (messageId==null){
            messageId= UUID.randomUUID().toString();
        }

        if (topic==null){
            throw  new RuntimeException("topic不能为空");
        }

        return new Message(messageId,topic,routingKey,attributes,delayMills,messageType);

    }



}
